﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TraceThisItem.Domain;
using System.Diagnostics;

namespace TraceThisItem.WebUI.Workflows
{
	public partial class Default : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void WorkflowDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

		protected void WorkflowGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{

		}

		protected void WorkflowDataSource_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
		{
			e.Cancel = true;

			var id = ((Workflow)e.InputParameters["item"]).Id;

			var hasItemTypes = Facade.Instance.GetWorkFlowById(id).ItemTypes.Count > 0;

			if (!hasItemTypes)
			{
				Facade.Instance.RemoveWorkflow(new Workflow { Id = id });
				Response.Redirect("~/Workflows");
			}
			MessageText.Text = "El workflow contiene tipos de item asociados. No se puede eliminar.";
		}

		protected void WorkflowGridView_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "Edit")
			{
				Response.Redirect("~/Workflows/Edit.aspx?id=" + e.CommandArgument);
			}
		}



	}
}